package com.cassi.rabbitmq.turorials.send;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author caasi
 * @since 2022-01-25 14:45:24
 */
@Component
public class Sender {
    
    @Value("${cassi.rabbit.exchange.order}")
    private String orderExchange;

    @Value("${cassi.rabbit.exchange.order-status}")
    private String orderStatusExchange;
    
    @Autowired
    @Qualifier("rabbitmq-template-vip")
    private RabbitTemplate rabbitTemplateVip;

    @Autowired
    @Qualifier("amqp-admin-vip")
    private AmqpAdmin amqpAdminVip;

    
    public void sendOrderMessage(String routingKey, Object message){
        //声明交换机
        Exchange exchange = new DirectExchange(orderExchange, true, false);
        amqpAdminVip.declareExchange(exchange);
        //声明队列 routingKey即为队列名称
        Queue queue = new Queue("ziyan.huangdong", true, false, false);
        amqpAdminVip.declareQueue(queue);
        //创建绑定
        Binding binding = new Binding("ziyan.huangdong", Binding.DestinationType.QUEUE, orderExchange, routingKey, null);
        amqpAdminVip.declareBinding(binding);
        //发送信息
        rabbitTemplateVip.convertAndSend(orderExchange, routingKey, message);
    }
    
    
}
